package com.jiajia.study;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @Title: TtlApplication
 * @Author zjj
 * @Package com.jiajia.study
 * @Date 2024/3/9 15:08
 * @description:
 */
@SpringBootApplication
@Slf4j
public class TtlApplication implements ApplicationRunner {

    @Resource
    RabbitTemplate rabbitTemplate;

    public static void main(String[] args) {
        SpringApplication.run(TtlApplication.class,args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Thread.sleep(3000);
        sendTtlMessage();
        sendTtlQueue();
    }

    private void sendTtlQueue() {
        Message message = new Message("ttl message by queue".getBytes(StandardCharsets.UTF_8));
        rabbitTemplate.convertAndSend("ttl.exchange.b","ttl.queue",message);
        log.info("send message to ttl queue success : {}",new Date());
    }

    private void sendTtlMessage() {
        MessageProperties messageProperties = new MessageProperties();
        // 设置消息属性，消息过期时间为 ： 15 秒
        messageProperties.setExpiration("15000");
        Message message = new Message("ttl message test".getBytes(StandardCharsets.UTF_8),messageProperties);
        rabbitTemplate.convertAndSend("ttl.exchange.a","ttl.message",message);
        log.info("send message success :{}",new Date());
    }
}
